package attend.action.codetable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import attend.com.Session;
import attend.dao.codetable.CodetableDao;
import dao.Paging.PagingDAO;
import exception.BaseException;

public class CodetableAction extends Session {
	
	private List<HashMap<String, String>> list = null; 
	private List<HashMap<String, String>> listGroupCode = null;
	
	private String checkedGroupCode;
	private String group_cd;
	private String cd;
	private String full_nm;
	private String groupCode;
	private String group_code_nm;
	private String nick_nm;
	private String en_full_nm;
	private String en_nick_nm;
	private String etc;
	private String checkedUseyn;
	private String use_yn;
	private String reg_id;
	private String table;
	
	private String[] checkSelect;
	
	private int count		= 0;
	private int rowCount	= 0; 				// 검색한  건수
	private int cur_page	= 1; 				// 현재 페이지 
	
	private int searchResult =0;
	
	CodetableDao 			codeDao 	= null; 
	PagingDAO 				pDao	 	= null;
	
	private String msg;		//message
	
	private String code;	//바꾸기전 하위코드
		
	
	public CodetableAction() {
	}
	/**
	 * @return
	 * @throws Exception
	 */
	public String commonCode() throws Exception {
		
		list			= new ArrayList<HashMap<String,String>>();
		codeDao			= new CodetableDao();
		listGroupCode	= new ArrayList<HashMap<String,String>>();
						
		codeDao.getGroupCodeName();               //대표코드, 대표코드명 가져오기
		listGroupCode	= codeDao.getList();
						
		return SUCCESS;
		
	}
	
	/**
	 * <pre>대표코드팝업창  OPEN</pre>
	 * @return
	 * @throws Exception
	 */
	public String addCommonCode() throws Exception {
		
		return SUCCESS;
		
	}
	
	/**
	 * <pre>하위코드 추가</pre>
	 * @return
	 * @throws Exception
	 */
	public String addSubCommonCode() throws Exception {
		
		codeDao = new CodetableDao();
		
		codeDao.retrieveGroupCodeList();
		listGroupCode	=	codeDao.getList();
		
		return SUCCESS;
		
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public String saveCommonCode() throws Exception {
		
		list = new ArrayList<HashMap<String, String>>();		
		codeDao = new CodetableDao();
				
		codeDao.getCode();		// 대표코드 자동생성 
		
		groupCode = codeDao.getGroupCode();
		
		codeDao.insertCommonCode(groupCode, group_code_nm, etc, checkedUseyn);   //대표코드 추가
		
		msg = codeDao.getMsg();
		System.out.println(msg + "           MSGMSGMSGMSGMSGMSG      saveCommonCode()");

		return SUCCESS;
		
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public String saveSubCommonCode() throws Exception {
		
		System.out.println("addSubCommonCode METHOD()");
		System.out.println(group_cd + "                   group_cd");
		System.out.println(cd + "                  하위코드");
		System.out.println(full_nm + "                코드명");
		System.out.println(checkedUseyn + "           사용여부");
		
		table = group_cd + "," + cd;
		
		listGroupCode = new ArrayList<HashMap<String, String>>();
		codeDao = new CodetableDao();
		
		codeDao.retrieveGroupCodeList();
		listGroupCode	=	codeDao.getList();
		
		codeDao.insertSubCommonCode(group_cd, cd, full_nm, nick_nm,  etc, checkedUseyn );   //하위코드 추가
		msg	=	codeDao.getMsg();

		return SUCCESS;
		
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public String getListForm() throws Exception {
		
		list 			= 	new ArrayList<HashMap<String,String>>();
		codeDao 		= 	new CodetableDao();
		listGroupCode 	= 	new ArrayList<HashMap<String,String>>();
						
		codeDao.getCommonCode(table);		//팝업창에 있어야 할 공통코드관리 데이터
		list = codeDao.getList();
		
		return SUCCESS;

	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public String updateCommonCode() throws Exception {
		
		codeDao = new CodetableDao();
		
		codeDao.saveCommonCode(group_cd, cd, use_yn, reg_id);  // 공통코드 수정
		
		return SUCCESS;
		
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public String updateGroupCode() throws Exception {
		
		codeDao = new CodetableDao();
		
		codeDao.saveGroupCode(group_cd, reg_id);  // 대표코드 사용여부가 'N'일때 공통코드 수정
		
		return SUCCESS;
		
	}
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		
		codeDao = new CodetableDao();
		list = new ArrayList<HashMap<String,String>>();
		listGroupCode = new ArrayList<HashMap<String,String>>();
		
		codeDao.getGroupCodeName();               //대표코드, 대표코드명 가져오기
		listGroupCode = codeDao.getList();
		
		codeDao.search_tot(checkedGroupCode, full_nm);                              
		
		rowCount = codeDao.getRowCount();
		
		if(rowCount > 0)
		{
			pDao = new PagingDAO(15,10,rowCount,cur_page);
			
			int start_row = pDao.getWriting_Start();
		    int end_row   = pDao.getWriting_End();
			
		    codeDao.search(checkedGroupCode, full_nm,start_row,end_row);       
		    list = codeDao.getList();
		}
				
		searchResult=codeDao.getSearchResult();	      // 조회결과

		return SUCCESS;
		
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String deleteCommonCode() throws Exception {

		list 			= new ArrayList<HashMap<String,String>>();
		codeDao 		= new CodetableDao();
		listGroupCode 	= new ArrayList<HashMap<String,String>>();
						
		codeDao.getGroupCodeName();               //대표코드, 대표코드명 가져오기
		
		listGroupCode = codeDao.getList();
		
		for (int i =0 ; i < checkSelect.length; i++){
							
			String[] 	arr 		=	checkSelect[i].split(",");
			String 		group_cd 	= arr[0];
			String 		cd 			= arr[1];
			
			if(cd.equals("0")) {
				
				codeDao.deleteSubCommonCode(group_cd);
				
			}else{
				
				codeDao.deleteCommonCode(group_cd, cd); 
				
			}	
		}
	
		return SUCCESS;
		
	}
	
	/**
	 * <pre>대표코드 수정화면 저장</pre>
	 * @return
	 * @throws BaseException
	 */
	public String saveUpdateCommonCode() throws BaseException {
		
		codeDao	=	new CodetableDao();
		
		codeDao.saveUpdateCommonCode(group_code_nm , use_yn , group_cd , etc);
		
		msg = codeDao.getMsg();
		
		listGroupCode	=	new ArrayList<HashMap<String,String>>();
		
		codeDao.getGroupCodeName();               //대표코드, 대표코드명 가져오기
		listGroupCode = codeDao.getList();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>하위공통코드 UPDATE</pre>
	 * @return
	 * @throws BaseException
	 */
	public String updateSubCommonCode() throws BaseException {
		
		listGroupCode	=	new ArrayList<HashMap<String,String>>();
		
		codeDao		=	new		CodetableDao();
		
		codeDao.updateSubCommmonCode(code , cd , full_nm ,group_cd , use_yn , etc);
		
		msg	=	codeDao.getMsg();
		
		codeDao.getGroupCodeName();               //대표코드, 대표코드명 가져오기
		listGroupCode = codeDao.getList();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>ajax dummy</pre>
	 * @return
	 * @throws BaseException
	 */
	public String ajaxDummy() throws BaseException {
		
		listGroupCode 	= 	new ArrayList<HashMap<String,String>>();
		
		//그룹코드 리스트
		codeDao			=	new CodetableDao();
		listGroupCode 	= 	codeDao.getList();
		
		return SUCCESS;
	}
	
	/*******************************************************************************************/

	public List<HashMap<String, String>> getList() {
		return list;
	}

	public void setList(List<HashMap<String, String>> list) {
		this.list = list;
	}

	public List<HashMap<String, String>> getListGroupCode() {
		return listGroupCode;
	}

	public void setListGroupCode(List<HashMap<String, String>> listGroupCode) {
		this.listGroupCode = listGroupCode;
	}

	public String getCheckedGroupCode() {
		return checkedGroupCode;
	}

	public void setCheckedGroupCode(String checkedGroupCode) {
		this.checkedGroupCode = checkedGroupCode;
	}

	public String getFull_nm() {
		return full_nm;
	}

	public void setFull_nm(String full_nm) {
		this.full_nm = full_nm;
	}

	public int getSearchResult() {
		return searchResult;
	}

	public void setSearchResult(int searchResult) {
		this.searchResult = searchResult;
	}

	public String getGroupCode() {
		return groupCode;
	}

	public void setGroupCode(String groupCode) {
		this.groupCode = groupCode;
	}

	public String getGroup_code_nm() {
		return group_code_nm;
	}

	public void setGroup_code_nm(String group_code_nm) {
		this.group_code_nm = group_code_nm;
	}

	public String getNick_nm() {
		return nick_nm;
	}

	public void setNick_nm(String nick_nm) {
		this.nick_nm = nick_nm;
	}

	public String getEn_full_nm() {
		return en_full_nm;
	}

	public void setEn_full_nm(String en_full_nm) {
		this.en_full_nm = en_full_nm;
	}

	public String getEn_nick_nm() {
		return en_nick_nm;
	}

	public void setEn_nick_nm(String en_nick_nm) {
		this.en_nick_nm = en_nick_nm;
	}

	public String getEtc() {
		return etc;
	}

	public void setEtc(String etc) {
		this.etc = etc;
	}

	public String getCheckedUseyn() {
		return checkedUseyn;
	}

	public void setCheckedUseyn(String checkedUseyn) {
		this.checkedUseyn = checkedUseyn;
	}

	public String getReg_id() {
		return reg_id;
	}

	public void setReg_id(String reg_id) {
		this.reg_id = reg_id;
	}

	public String getGroup_cd() {
		return group_cd;
	}

	public void setGroup_cd(String group_cd) {
		this.group_cd = group_cd;
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public String getCd() {
		return cd;
	}

	public void setCd(String cd) {
		this.cd = cd;
	}

	public String getUse_yn() {
		return use_yn;
	}

	public void setUse_yn(String use_yn) {
		this.use_yn = use_yn;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getCur_page() {
		return cur_page;
	}

	public void setCur_page(int cur_page) {
		this.cur_page = cur_page;
	}

	public CodetableDao getCodeDao() {
		return codeDao;
	}

	public void setCodeDao(CodetableDao codeDao) {
		this.codeDao = codeDao;
	}

	public PagingDAO getPDao() {
		return pDao;
	}

	public void setPDao(PagingDAO dao) {
		pDao = dao;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String[] getCheckSelect() {
		return checkSelect;
	}

	public void setCheckSelect(String[] checkSelect) {
		this.checkSelect = checkSelect;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	/*******************************************************************************************/
}